package com.atguigu.controller;

import com.atguigu.model.cmn.Dict;
import com.atguigu.result.Result;
import com.atguigu.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@Slf4j
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    //根据上一级id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        log.info("入参：{}", id);
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }

    @ApiOperation(value="导出到excel")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @ApiOperation(value = "从excel中导入导入数据字典")
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }

    //根据dictCode获取当前节点下所有的下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    //根据dictcode或value查询数据字典的根结点数据
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    //根据value查询数据字典中的子节点数据（子节点的dictCode没有值）
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);
        return dictName;
    }

    @GetMapping("/getDictMapByVal")
    public List<Map<String, Dict>> getDictMapByVal(@RequestParam List<String> valueList){
        return dictService.getDictMapByVal(valueList);
    }

}

